<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<SCRIPT TYPE="text/javascript">
<!--
function popup(mylink, windowname)
{
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
   href=mylink;
else
   href=mylink.href;
window.open(href, windowname, 'width=500,height=400,scrollbars=yes');
return false;
}
//-->
</SCRIPT>

<?php
include 'header.php';
include 'menu.php';
?>


</head>

<body>
<br><br>
<?php

//function to get xml data,  the function is called with a name of a domain or po

function getSettings($object)

{
	$configfile='/etc/opt/fbc/fbc.xml';
	$xml = simplexml_load_file($configfile);

	//loop thru the servers, then load xml data and an array for the matching server name

	foreach ($xml->server as $s)
	{
		if ($s->name == $object)
		{
			$gwname = $s->name;
			$ipaddr = $s->ipaddr;
			$remotepass = $s->remotepass;
			$remotepath = $s->remotepath;
			$remoteuser = $s->remoteuser;
			$mounttype = $s->mounttype;
		

			$objectsettings = array(
				"gwname" => $gwname, 
				"ipaddr" => $ipaddr,
				"remotepass" => $remotepass,
				"remotepath" => $remotepath,
				"remoteuser" => $remoteuser,
				"mounttype" => $mounttype
			);

			// return the array to the caller
				
			return $objectsettings;
		}
	}
}

//Get the dom or po name from the url link

$name = $_GET[name];

//call teh function to load the xml data

$settings = getSettings($name);


// function to remove server if "delete" was selected

if (isset($_POST['delserver']))
{
	$object = $_POST['gname'];
	
	//load teh xml file again

    $configfile='/etc/opt/fbc/fbc.xml';
  	$x = simplexml_load_file($configfile);
	
	//loop thru to find the right dom/po
    foreach ($x->server as $s)
   	{
        if ($s->name == $object)
        {

		// this part sux,  simplexml won't let me delete a node
		//  so we have to load the node into a dom object
		//  and delete it that way.
        $dom=dom_import_simplexml($s);
        $dom->parentNode->removeChild($dom);
		}
	}
		//write the file back to disk and refresh
        file_put_contents($configfile, $x->asXML());
		header("Location:index.php");
}


// function to change data if "Saved was selected
// See comments for similar function in globalsettings.php
// for details 

if (isset($_POST['save']))
{

	$configfile='/etc/opt/fbc/fbc.xml';
	$xml = simplexml_load_file($configfile);

	$gwname = $_POST['gwname'];
	$ipaddr = $_POST['ipaddr'];
	$remotepath = $_POST['remotepath'];
	$remoteuser = $_POST['remoteuser'];
	$remotepass = $_POST['remotepass'];
	$mounttype = $_POST['mounttype'];

	foreach ($xml->server as $s)
	{
		if ($s->name == $gwname)
		{
			$s->ipaddr = $ipaddr;
			$s->remotepath = $remotepath;
			$s->remoteuser = $remoteuser;
			$s->remotepass = $remotepass;
			$s->mounttype = $mounttype;

			file_put_contents($configfile, $xml->asXML()) or die("save failed");
		}
	}			
			
	$settings = getSettings($gwname);
}

?>

<!-- create the form -->
<br>
<b>Settings for <?php echo $name;?></b>
<hr>
<form id='form1' name='form1' method='post' action='<?php echo $_SERVER['PHP_SELF'];?>'/>
<input name='gwname' type='hidden' value="<?php echo $settings[gwname];?>" />
<table>
<tr><td><b>Server IP: </b></td>
<td><input name='ipaddr' type='text' value="<?php echo $settings[ipaddr];?>" /></td></tr>
<tr><td><b>Remote Path:</b></td>
<td><input name='remotepath' type='text' value="<?php echo $settings[remotepath];?>" /></td></tr>
<tr><td><b>Remote User:</b></td>
<td><input name='remoteuser' type='text' value="<?php echo $settings[remoteuser];?>" /></td></tr>
<tr><td><b>Remote Password:</b></td>
<td><input name='remotepass' type='password' value="<?php echo $settings[remotepass]; ?>" /></td></tr>
<tr><td><b>Mount Type:</b></td>
<td>Samba<input type='radio' value='Samba' name='mounttype'
	<?php if ($settings[mounttype] == "samba" or $settings[mounttype] == "Samba") echo 'checked';?> >
    NCP<input type='radio' value='NCP' name='mounttype'
	<?php if ($settings[mounttype] == "NCP") echo 'checked';?> ></td></tr>
</table>
<p>
<table>
<tr><td><input name='save' type='submit' value='Save Changes' /></td>
<td><input name='delserver' type='submit' value='Delete <?php echo $settings[gwname];?>'></td>
<input name='gname' value='<?php echo $settings[gwname];?>' type='hidden'/>
</form>
<td><button onClick="return popup('help.html', 'serverhelp')">Help</button></td></tr>
</table>
<br><br><br>
</p>
</body></html>
<?php include 'footer.php'; ?>	



